<template>
  <div class="container">
    <div class="c_container">
      <div class="video-player">
        <!-- Video playback box -->
        <video src='@/assets/test.mp4' controls></video>
      </div>
      <div class="form_item_button_2">
        <el-form-item>
          <div class="-button2--">
            <el-button type="primary" @click="resetForm(ruleFormRef)">保存照片</el-button>
          </div>
          <div class="-button2--">
            <el-button type="primary" @click="resetForm(ruleFormRef)">选择摄像头</el-button>
          </div>
        </el-form-item>
      </div>
    </div>

    <div class="info-form">
      <!-- Information entry form -->
      <h2>信息录入</h2>
      <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="120px"
          class="demo-ruleForm"
          :size="formSize"
          status-icon="true"
      >

        <div class="form_item">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name" placeholder="name"/>
          </el-form-item>
        </div>

        <div class="form_item">
          <el-form-item label="性别" prop="sex">
            <el-select v-model="ruleForm.sex" placeholder="your sex">
              <el-option label="男" value="male" />
              <el-option label="女" value="female" />
            </el-select>
          </el-form-item>
        </div>

        <div class="form_item">
          <el-form-item label="年龄" prop="age">
            <el-input v-model="ruleForm.age" placeholder="your age" />
          </el-form-item>
        </div>

        <div class="form_item">
          <el-form-item label="住址" prop="address">
            <el-input v-model="ruleForm.address" placeholder="address"/>
          </el-form-item>
        </div>

        <div class="form_item">
          <el-form-item label="电话" prop="phone">
            <el-input v-model="ruleForm.phone" placeholder="phone" />
          </el-form-item>
        </div>

        <div class="form_item">
          <el-form-item label="邮件" prop="email">
            <el-input v-model="ruleForm.email" placeholder="email" />
          </el-form-item>
        </div>

        <div class="form_item_button_1">
          <el-form-item>
            <div class="-button1--">
              <el-button type="primary" @click="submitForm(ruleFormRef)">
                提交信息
              </el-button>
            </div>
            <div class="-button1--">
              <el-button type="primary" @click="resetForm(ruleFormRef)">重新提交</el-button>
            </div>
          </el-form-item>
        </div>
      </el-form>

    </div>

  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'

interface RuleForm {
  name: string
  address: string
  phone: string
  email: string
  age: string
  sex: string
}

const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
// const ruleForm: RuleForm = reactive({
//   name: '',
//   address: ' ',
//   phone: ' ',
//   email: ' ',
//   age: ' ',
//   sex: ' ' })
const ruleForm = reactive<RuleForm>({
  name: '',
  address: '',
  phone: '',
  email: '',
  age: '',
  sex: ''
})

const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: '请输入姓名！', trigger: 'blur' },
    { min: 3, message: '姓名至少三个字符！', trigger: 'blur' },
  ],
  address: [
    {
      required: true,
      message: '请输入住址！',
      trigger: ['blur', 'change'],
      validator: (rule, value, callback) => {
        if (value !== '') {
          callback();
        } else {
          callback(new Error('请输入住址！！'));
        }
      },
    },
  ],
  age: [
    {
      required: true,
      message: '年龄不可为空！',
      trigger: ['blur', 'change'],
      validator: (rule, value, callback) => {
        if (value !== '') {
          callback();
        } else {
          callback(new Error('年龄不可为空！'));
        }
      },
    },
    {
      type: 'number',
      min: 0,
      max: 100,
      message: '请重新输入年龄！',
      trigger: ['blur', 'change'],
      validator: (rule, value, callback) => {
        if (value >= 0 && value <= 100) {
          callback();
        } else if (value === '') {
          callback(new Error('年龄不可为空！'));
        } else {
          callback(new Error('请重新输入年龄！'));
        }
      },
    },
  ],

  phone: [
    {
      required: true,
      message: '请输入电话号码！',
      trigger: ['blur', 'change'],
      validator: (rule, value, callback) => {
        if (value !== '') {
          callback();
        } else {
          callback(new Error('请输入电话号码！'));
        }
      },
    },
    {
      required: true,
      pattern: /^\d{11}$/,
      message:'输入错误！请重新输入！',
      trigger: ['blur','change'],
      validator: (rule, value, callback) => {
        if (/^\d{11}$/.test(value)) {
          callback();
        } else if (value === '') {
          callback(new Error('请输入电话号码！'));
        } else {
          callback(new Error('输入错误！请重新输入！'));
        }
      },

    }
  ],
  sex: [
    {
      required: true,
      message: '请选择性别！',
      trigger: ['blur','change'],
      validator: (rule, value, callback) => {
        if (value !== '') {
          callback();
        } else {
          callback(new Error('请选择性别！'));
        }
      },
    },
  ],
  email: [
    { required: true,
      message: '请输入邮件地址！',
      trigger: ['blur','change'],
      validator: (rule, value, callback) => {
        if (value !== '') {
          callback();
        } else {
          callback(new Error('请输入邮件地址！！'));
        }
      },
    },
    {
      pattern: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/,
      required: true,
      message: '请输入有效的邮件地址!',
      trigger: ['blur','change'],
      validator: (rule, value, callback) => {
        if (/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/.test(value)) {
          callback();
        } else if (value === '') {
          callback(new Error('请输入邮件地址！'));
        } else {
          callback(new Error('请输入有效的邮件地址!'));
        }
      },

    },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>
<style>
.container {
  display: flex;
  justify-content: space-between;
//align-items: flex-start;
  margin-top: 25px;
  height: 100%;
  width: 100%;
//justify-content: flex-start;
  align-items: stretch;
  background-image: url('src/assets/img_1.png');
  background-size: cover;
  background-repeat: no-repeat;
//justify-content: space-between;
//align-items: flex-start;

}
.c_container {
  display: flex;
  flex-wrap: wrap;
  width: 650px;
  height: 520px;
  justify-content: center;
//align-content: center;
  margin-right: 20px;
  margin-top: 10px;
}

.video-player {
  flex-basis: 100%;
  margin-top: 20px;
  width: 640px;
  height: 480px;
}

.form_item_button_2 {
  display: flex;
  justify-content: center;
  width: 100%;
  margin-top: 5px;
}

.-button2-- {
  display: flex;
  justify-content: center;
  margin-right: 50px;
}
.info-form {
  flex: 1;
  border: 1px solid #ccc;
  padding: 20px;
//padding-left: 5px;
//order: 2;
  height: 450px;
  font-size: 20px;
  background: #C6E2FF;
  color: #126ca5;

//text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  text-align: center;
//background: linear-gradient(#82c3dd, #068d9c);
}
.form_item_button_1 {
//margin-top: 5px;
  display: flex;

}
.-button1-- {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-top: 10px;
  margin-right: 10px;
  flex-basis: 40px;
//align-items: flex-start;
}

</style>